package com.sjm.suchbear.easymap;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.easymap.Adapter.HistoryAdapter;
import com.sjm.suchbear.easymap.Model.PatrolListModel;
import com.sjm.suchbear.easymap.Service.iHttpService;
import com.sjm.suchbear.easymap.Util.DialogLoading;
import com.sjm.suchbear.easymap.Util.HttpUtils;
import com.sjm.suchbear.easymap.Util.SP;
import com.sjm.suchbear.easymap.View.MyListView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;
import zilla.libcore.util.Util;

public class HistoryActivity extends AppCompatActivity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.lv_historyList)
    MyListView lvHistoryList;

    private iHttpService service;
    private HistoryAdapter historyAdapter;
    private PatrolListModel mpatrolListModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        tvListTitle.setText("巡河记录");
        service = HttpUtils.createService(this);
        initData();
        lvHistoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(HistoryActivity.this, MainActivity.class);
                intent.putExtra("model", 5);
                intent.putExtra("patrolId", mpatrolListModel.getPatrolList().get(position));
                startActivity(intent);
            }
        });
    }

    private void initData() {
        gethistorys();
    }

    @OnClick(R.id.ll_header_back)
    public void onViewClicked() {
        finish();
    }

    private void gethistorys() {
        service.getPatrolList(SP.getUid(getBaseContext()))
                .subscribeOn(Schedulers.io())//指定网络请求在IO线程
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        DialogLoading.StartLodingDialog(HistoryActivity.this);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())//显示Dialog在主线程中
                .observeOn(AndroidSchedulers.mainThread())//显示数据在主线程
                .subscribe(new Subscriber<PatrolListModel>() {
                    @Override
                    public void onCompleted() {
                        DialogLoading.StopLodingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        DialogLoading.StopLodingDialog();
                        Util.toastMsg("网络连接失败");
                    }

                    @Override
                    public void onNext(PatrolListModel patrolListModel) {
                        mpatrolListModel = patrolListModel;
                        if (patrolListModel.getPatrolList().size() > 0) {
                            historyAdapter = new HistoryAdapter(getBaseContext(), patrolListModel);
                            lvHistoryList.setAdapter(historyAdapter);
                            historyAdapter.notifyDataSetChanged();
                        } else {
                            Util.toastMsg("暂无巡河记录");
                        }
                    }
                });
    }
}
